package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PhysicalManagement;
import com.ruoyi.system.service.IPhysicalManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 四级联考体能测评Controller
 * 
 * @author ruoyi
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/system/physical")
public class PhysicalManagementController extends BaseController
{
    @Autowired
    private IPhysicalManagementService physicalManagementService;

    /**
     * 查询四级联考体能测评列表
     */
    @PreAuthorize("@ss.hasPermi('system:physical:list')")
    @GetMapping("/list")
    public TableDataInfo list(PhysicalManagement physicalManagement)
    {
        startPage();
        List<PhysicalManagement> list = physicalManagementService.selectPhysicalManagementList(physicalManagement);
        return getDataTable(list);
    }

    /**
     * 导出四级联考体能测评列表
     */
    @PreAuthorize("@ss.hasPermi('system:physical:export')")
    @Log(title = "四级联考体能测评", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PhysicalManagement physicalManagement)
    {
        List<PhysicalManagement> list = physicalManagementService.selectPhysicalManagementList(physicalManagement);
        ExcelUtil<PhysicalManagement> util = new ExcelUtil<PhysicalManagement>(PhysicalManagement.class);
        util.exportExcel(response, list, "四级联考体能测评数据");
    }

    /**
     * 获取四级联考体能测评详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:physical:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(physicalManagementService.selectPhysicalManagementById(id));
    }

    /**
     * 新增四级联考体能测评
     */
    @PreAuthorize("@ss.hasPermi('system:physical:add')")
    @Log(title = "四级联考体能测评", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PhysicalManagement physicalManagement)
    {
        return toAjax(physicalManagementService.insertPhysicalManagement(physicalManagement));
    }

    /**
     * 修改四级联考体能测评
     */
    @PreAuthorize("@ss.hasPermi('system:physical:edit')")
    @Log(title = "四级联考体能测评", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PhysicalManagement physicalManagement)
    {
        return toAjax(physicalManagementService.updatePhysicalManagement(physicalManagement));
    }

    /**
     * 删除四级联考体能测评
     */
    @PreAuthorize("@ss.hasPermi('system:physical:remove')")
    @Log(title = "四级联考体能测评", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(physicalManagementService.deletePhysicalManagementByIds(ids));
    }

    /**
     * 获取导入模板
     */
    @PreAuthorize("@ss.hasPermi('system:physical:import')")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<PhysicalManagement> util = new ExcelUtil<PhysicalManagement>(PhysicalManagement.class);
        util.importTemplateExcel(response, "四级联考体能测评数据");
    }

    /**
     * 导入四级联考体能测评数据
     */
    @PreAuthorize("@ss.hasPermi('system:physical:import')")
    @Log(title = "四级联考体能测评", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<PhysicalManagement> util = new ExcelUtil<PhysicalManagement>(PhysicalManagement.class);
        List<PhysicalManagement> physicalList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = physicalManagementService.importPhysicalData(physicalList, updateSupport, operName);
        return success(message);
    }
    
    /**
     * 生成递补体能测试名单
     */
    @PreAuthorize("@ss.hasPermi('system:physical:edit')")
    @Log(title = "生成递补体能测试名单", businessType = BusinessType.UPDATE)
    @PostMapping("/generateDbPhysicalList")
    public AjaxResult generateDbPhysicalList()
    {
        String operName = getUsername();
        int count = physicalManagementService.generateDbPhysicalList(operName);
        return success("成功生成递补体能测试名单，从成绩合格名单中递补" + count + "人");
    }

    /**
     * 获取四级联考体能测评统计数据
     */
    @PreAuthorize("@ss.hasPermi('system:physical:list')")
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        return success(physicalManagementService.getPhysicalStatistics());
    }
}
